package com.zl.learning.object;

import org.junit.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;

public class Test2 {

    class Animal {
        public int legs = 4;
    }

    class Dog extends Animal {

    }

    @Test
    public void test2() {
        List<Animal> animals = new ArrayList<>();
        animals.add(new Animal());
        animals.add(new Dog());
        Map<String, List<? extends Animal>> animalMap = new HashMap<>();
        List<Dog> dogs = new ArrayList<>();
        animalMap.put("a", animals);
        animalMap.put("b", dogs);
    }

    @Test
    public void test1() {
        User user = User.builder().id(1).username("zhangsan").build();
        System.out.println(user);
    }

    @Test
    public void test3() {
        Map<Integer, String> map = Map.of(1, "a", 2, "b", 3, "c");
        map.forEach((k, v) -> System.out.println(k + "--" + v));
    }

    /**
     * super 只可以写
     * extends 只可以读
     */
    @Test
    public void test4() {
        List<? super String> list = new ArrayList<>();
        list.add("aaa");
//        list.forEach(System.out::println);
//        getData(list);
        list = new ArrayList<String>();
    }

    @Test
    public void test5() {
        User user = new User();
        change(user);
        System.out.println(user.getId());
    }

    private void change(User user) {
        user = new User();
        user.setId(1);
    }

    @Test
    public void test6() {
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        for (int i = 0; i < list.size(); i++) {
            list.remove(i);
        }
    }

    @Test
    public void test7() {
//        List<?> list = new ArrayList<>();
//        list.add("aa");
        List<? extends String> list = List.of("aaa", "bbbb");
//        list.add("aaa");
        String str = list.get(0);
//        List<? super String> list = new ArrayList<>();
//        list.add("aaa");
//        String a = list.get(0);
    }

    @Test
    public void test8() {
//        Integer i1 = 1000;
//        Integer i2 = 1000;
//        System.out.println(i1.equals(i2));
        String s1 = "aaaa";
        String s2 = "aaaa";
        System.out.println(s1==s2);
    }

    @Test
    public void test9() {
        String a = new String("11");
        String b = "11";
        String c = a.intern();
        System.out.println(a==b);//false
        System.out.println(b==c);//true
        System.out.println(a==c);//false
    }

    @Test
    public void test10() {
        String a = new String("11");
        String b = "11";
        String c = a.intern();
//        String c = a.intern();
        System.out.println(a==b);//false
        System.out.println(b==c);//true
//        System.out.println(a==c);//false
    }

    @Test
    public void test11() {
        String val = "-1E-16";
        double a = Double.parseDouble(val);
        System.out.println(a);
    }

    public void getData(Predicate<? super String> predicate) {
        predicate.test("aa");
    }

}
